package com.example.hospitalmanagement.viewmodel

import android.util.Log
import androidx.lifecycle.ViewModel
import com.example.hospitalmanagement.BaseApplication
import com.example.hospitalmanagement.bean.DrugPriceData
import rx.Observable
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers

class DrugPriceViewModel:ViewModel() {
    fun getMedicine(
        map: MutableMap<String, String>,
        listener:(dataList:MutableList<DrugPriceData>) ->Unit
    ){
        val dataList:MutableList<DrugPriceData> = mutableListOf()
        BaseApplication.iDrugPriceRequest.getDrugPriceByName(map)
            .flatMap {
                Observable.from(it.data)
            }?.map {
                DrugPriceData(
                    placeName = it.placeName,
                    productName = it.productName,
                    salePrice = it.salePrice,
                    false
                )
            }?.subscribeOn(Schedulers.io())
            ?.observeOn(AndroidSchedulers.mainThread())
            ?.subscribe({
                dataList.add(it)
            },{
                Log.e("HospitalViewModel",it.message,it)
            },{
                listener(dataList)
            })
    }
}